package com.longsys.framework.server.mongo;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
public class MongoAggregateRepository {

    @Autowired
    private MongoTemplate mongoTemplate;

    public void save(MongoAggregateEntity mongoAggregateEntity) {
        String system = mongoAggregateEntity.getSystem();
        String aggregateName = mongoAggregateEntity.getAggregateName();

        Map<String, Object> aggregateMap = mongoAggregateEntity.getAggregateMap();
        String documentName = MongoCollectionNameUtil.getAggregateCollectionName(system, aggregateName);
        mongoTemplate.insert(aggregateMap, documentName);

        ObjectId objectId = (ObjectId)aggregateMap.get("_id");
        mongoAggregateEntity.set_id(objectId);
    }
}
